﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ExportItem2Lua
{
    public partial class FrmOutput : Form
    {
        public FrmOutput()
        {
            InitializeComponent();
        }

        private DataGridView Grid = new DataGridView();
        public FrmOutput(List<string> Columns, DataGridView dgv)
        {
            InitializeComponent();
            foreach (string str in Columns)
            {
                lbAllColumn.Items.Add(str);
            }
            Grid = dgv;
        }

        private void FrmOutput_Load(object sender, EventArgs e)
        {

        }

        private void Tool_OK_Click(object sender, EventArgs e)
        {
            if (Grid.Rows.Count < 1)
            {
                MessageBox.Show("需要导出的数据为空，导出失败。", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "输出Lua脚本";
                sfd.Filter = "Lua脚本(*.lua)|*.lua";
                string filePath = string.Empty;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    filePath = sfd.FileName;
                    if (!File.Exists(filePath))
                    {
                        File.Create(filePath).Close();
                    }
                    StreamWriter sw = File.AppendText(filePath);

                    ProgressBar1.Visible = true;
                    ProgressBar1.Value = 0;
                    ProgressBar1.Maximum = Grid.Rows.Count;
                    string strResult = "";
                    if (Grid.Rows.Count > 0)
                    {
                        for (int i = 0; i < Grid.Rows.Count; i++)
                        {
                            ProgressBar1.Value = i;
                            strResult = txtCmd.Text.Trim();
                            for (int j = 0; j < lbSelColumn.Items.Count; j++)
                            {
                                strResult = strResult.Replace("{" + j.ToString() + "}", Grid.Rows[i].Cells[lbSelColumn.Items[j].ToString()].Value.ToString());
                                //lbAllColumn.Items.Add(Grid.Rows[i].Cells[lbSelColumn.Items[j].ToString()].Value.ToString());
                            }
                            //lbAllColumn.Items.Add(strResult);
                            sw.WriteLine(strResult);
                            sw.Flush();
                        }
                        ProgressBar1.Visible = false;
                    }
                    sw.Close();
                    sw.Dispose();
                }
                MessageBox.Show("导出Lua脚本成功.", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cboHistory.Items.Add(txtCmd.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Tool_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            if (lbAllColumn.Text != "")
            {
                lbSelColumn.Items.Add(lbAllColumn.Text);
                lbAllColumn.Items.Remove(lbAllColumn.Text);
            }
        }

        private void btnAllRight_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lbAllColumn.Items.Count; i++)
            {
                lbSelColumn.Items.Add(lbAllColumn.Items[i].ToString());
            }
            lbAllColumn.Items.Clear();
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            if (lbSelColumn.Text != "")
            {
                lbAllColumn.Items.Add(lbSelColumn.Text);
                lbSelColumn.Items.Remove(lbSelColumn.Text);
            }
        }

        private void btnAllLeft_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lbSelColumn.Items.Count; i++)
            {
                lbAllColumn.Items.Add(lbSelColumn.Items[i].ToString());
            }
            lbSelColumn.Items.Clear();
        }

        private void lbAllColumn_DoubleClick(object sender, EventArgs e)
        {
            if (lbAllColumn.Text != "")
            {
                lbSelColumn.Items.Add(lbAllColumn.Text);
                lbAllColumn.Items.Remove(lbAllColumn.Text);
            }
        }

        private void lbSelColumn_DoubleClick(object sender, EventArgs e)
        {
            if (lbSelColumn.Text != "")
            {
                lbAllColumn.Items.Add(lbSelColumn.Text);
                lbSelColumn.Items.Remove(lbSelColumn.Text);
            }
        }

        private void Tool_Help_Click(object sender, EventArgs e)
        {
            string strMsg = "导出的脚本中所有的变量都用{number}表示。\r\n\r\n如果你选择了两个字段变量，则可以表示为：{0},{1} 依据所选择字段的顺序。\r\n\r\n在导出的时候，软件会自动从表中读取对应的字段信息进行填充。";
            MessageBox.Show(strMsg, "帮助", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
